package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.mybatis.CollectionMybatisLanguageDriver;
import com.ds.lens.data.service.dao.entity.LensDashboard;
import com.ds.lens.data.service.dao.entity.LensDashboardFavorite;
import com.ds.lens.data.service.dao.sql.provider.LensDashboardSqlProvider;
import com.ds.lens.data.vo.in.v2.dashboard.LensDashboardListInVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-05
 */

@Repository
public interface LensDashboardDao {

    String COLUMNSWITHOUTPANEL = " id, name, type, shared, opened_count, expert_template, is_delete,application_id, creator, created_time, mender, modified_time, monitor_id,monitor_version, event_id,monitor_window_time  ";
    String COLUMNSWITHPANEL = " id, name, type, shared, opened_count, expert_template,panel_content, is_delete,application_id, creator, created_time, mender, modified_time, monitor_id,monitor_version, event_id,monitor_window_time ";

    @InsertProvider(type = LensDashboardSqlProvider.class, method = "insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(LensDashboard record);

    @Select({
            "select",
            COLUMNSWITHPANEL,
            "from lens_dashboard",
            "where is_delete=0 and id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "lensDashboard", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER),
            @Result(column = "shared", property = "shared", jdbcType = JdbcType.INTEGER),
            @Result(column = "opened_count", property = "openedCount", jdbcType = JdbcType.BIGINT),
            @Result(column = "expert_template", property = "expertTemplate", jdbcType = JdbcType.INTEGER),
            @Result(column = "application_id", property = "applicationId", jdbcType = JdbcType.INTEGER),
            @Result(column = "panel_content", property = "panelContent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "monitor_version", property = "monitorVersion", jdbcType = JdbcType.INTEGER),
            @Result(column = "monitor_window_time", property = "monitorWindowTime", jdbcType = JdbcType.INTEGER),
            @Result(column = "event_id", property = "eventId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "creator", property = "creator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_delete", property = "isDelete", jdbcType = JdbcType.BIT)
    })
    LensDashboard selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensDashboardSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensDashboard record);


    @Select({"<script>",
            "select",
            " d.id, name, type, shared, opened_count, expert_template, is_delete,application_id, creator, created_time, mender, modified_time, monitor_id,monitor_version, event_id,monitor_window_time ",
            " from lens_dashboard d left JOIN lens_dashboard_favorite f on d.id =f.dashboard_id and f.collected_by = #{userName} ",
            " where d.is_delete=0 ",
            "<if test=\"dashboard.applicationIds!=null and dashboard.applicationIds.size >0\">",
            " and application_id in ",
            "<foreach collection='dashboard.applicationIds' item='applicationId' separator=',' open='(' close=')' >",
            "#{applicationId}",
            "</foreach>",
            "</if>",
            " and (d.creator = #{userName} or d.shared = 1 )",
            "<if test=\"dashboard.name!=null and dashboard.name != '' \">",
            " and name like concat(#{dashboard.name},'%')",
            "</if>",
            "<if test=\"dashboard.type!=null and dashboard.type != '' \">",
            " and type = #{dashboard.type} ",
            "</if>",
            "<if test=\"dashboard.sort != null \"> ",
            " order by ${dashboard.sort.field} ${dashboard.sort.order}",
            "</if>",
            "</script>"
    })
    @ResultMap("lensDashboard")
    Page<LensDashboard> queryDashboard(@Param("dashboard") LensDashboardListInVO lensDashboardListInVO, @Param("userName") String userName);


    @Select({"<script>",
            "select",
            COLUMNSWITHOUTPANEL,
            " from lens_dashboard d",
            " where d.is_delete=0 ",
            "<if test=\"dashboard.applicationIds!=null and dashboard.applicationIds.size >0\">",
            " and application_id in ",
            "<foreach collection='dashboard.applicationIds' item='applicationId' separator=',' open='(' close=')' >",
            "#{applicationId}",
            "</foreach>",
            "</if>",
            " and type = 2 and monitor_id =#{dashboard.monitorId} and (d.creator = #{userName} or d.shared = 1 )",
            "<if test=\"dashboard.name!=null and dashboard.name != '' \">",
            " and name like concat(#{dashboard.name},'%')",
            "</if>",
            "<if test=\"dashboard.sort != null \"> ",
            " order by ${dashboard.sort.field} ${dashboard.sort.order}",
            "</if>",
            "<if test=\"dashboard.sort == null \"> ",
            " order by monitor_version desc,opened_count desc,modified_time desc ",
            "</if>",
            "</script>"
    })
    @ResultMap("lensDashboard")
    Page<LensDashboard> queryRecommendedDashboard(@Param("dashboard") LensDashboardListInVO lensDashboardListInVO,  @Param("userName") String userName);



    @Select({
            "select ",
            "id,dashboard_id,collected_by ",
            "from lens_dashboard_favorite ",
            "where collected_by = #{userName}"
    })
    @Results(id = "lensDashboardFavorite", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "dashboard_id", property = "dashboardId", jdbcType = JdbcType.BIGINT),
            @Result(column = "collected_by", property = "collectedBy", jdbcType = JdbcType.VARCHAR)
    })
    List<LensDashboardFavorite> queryFavoriteList(@Param("userName") String userName);


    @Insert({"insert into lens_dashboard_favorite (dashboard_id, collected_by)", "values ",
            " (#{dashboardId}, #{collectedBy})"})
    Long saveFavorite(@Param("dashboardId") Long dashboardId, @Param("collectedBy") String collectedBy);

    @Lang(CollectionMybatisLanguageDriver.class)
    @Delete({
            "delete from  lens_dashboard_favorite where dashboard_id in (#{ids}) " ,
            "<if test=\"userName!=null and userName != '' \">",
            " and collected_by = #{userName}",
            "</if>",
    })
    Long removeFavorite(@Param("ids") List<Long> ids, @Param("userName")String userName);

    @Update({"update lens_dashboard set opened_count=opened_count + 1",
            " where id=#{id} and creator <> #{userName}"
    })
    Long addOpenedCount(@Param("userName") String userName, @Param("id") Long id);

    @Lang(CollectionMybatisLanguageDriver.class)
    @Update({
            "update lens_dashboard set is_delete=1",
            "where id in (#{ids}) and creator = #{userName}"
    })
    int deleteDashboard(@Param("ids") List<Long> ids, @Param("userName") String userName);

    @Update({
            "update lens_dashboard set shared= #{shared}",
            "where id = #{id} and creator = #{userName}"
    })
    int shareDashboard(@Param("id") Long id, @Param("shared") Integer shared, @Param("userName") String userName);

    @Select({
            "select count(1) from lens_dashboard_favorite where dashboard_id = #{id} and collected_by = #{userName} "
    })
    int isCollected(@Param("id") Long id,@Param("userName") String userName);

    @Select({
            "select" ,
                     COLUMNSWITHPANEL ,
                    " from lens_dashboard where is_delete=0 and type=2 and creator = #{userName} and event_id = #{eventId} for update"
    })
    LensDashboard isExists(@Param("userName") String userName, @Param("eventId") String eventId );



}
